/**
* This class lets you handle class smileys and animated ones inside a paragraph in Flash Player 10
* @author Thibault Imbert - bytearray.org
* @version 0.2 - Memory optimizations
*/

package org.bytearray.tools
{	
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import flash.utils.ByteArray;
	import flash.text.engine.ElementFormat;
	import flash.text.engine.TextBlock;
	import flash.text.engine.TextElement;
	import flash.text.engine.TextLine;
	import flash.text.engine.GraphicElement;
	import flash.text.engine.GroupElement;
	import flash.text.engine.ContentElement;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	import org.bytearray.gif.player.GIFPlayer;
	
	public class SmileyRenderer extends Sprite
	{
		private var mapper:Dictionary;
		private var textContent:String;
		public var reg:RegExp/* = /(:[-][)|(pDd*]|:[)|(pDd*])/g*/;
		private var content:Array;
		private var groupVector:Vector.<ContentElement> = new Vector.<ContentElement>();
		private var textBlock:TextBlock = new TextBlock();
		private var groupElement:GroupElement = new GroupElement();
		private var element:ContentElement;
		private var smiley:DisplayObject;
		private var formating:ElementFormat;
		private var paragraphWidth:int;

		public var X_OFFSET:uint;
		public var Y_OFFSET:uint;
		
		public var standardHeight:Number;
		public var standardDistance:Number 
		
		public var firstLine:TextLine;
		
		public function SmileyRenderer ( dictionary:Dictionary, format:ElementFormat, width:int )
		{
			formating = format;	
			mapper = dictionary;
			paragraphWidth = width;
		}	
		
		public function set text ( content:String ):void
		{
			textContent = content;
			render();
		}
		
		public function get text ():String
		{
			return textContent;
		}
		
		private function render():void
		{
			while ( numChildren ) 
				removeChildAt(0);
			content = textContent.split(reg);
			var lng:int = content.length;
			var currentString:String;
			var description:XML;
			groupVector.length = 0;
			
			for each ( currentString in content )
			{	
				if ( mapper[currentString] != null )
				{
					description = describeType ( mapper[currentString] );
					
					if ( describeType(mapper[currentString]).factory.extendsClass.(@type==getQualifiedClassName(ByteArray)).length() > 0 )
					{
						smiley = new GIFPlayer();
						(smiley as GIFPlayer).loadBytes ( new mapper[currentString] as ByteArray );
						element = new GraphicElement ( smiley, (smiley as GIFPlayer).maxWidth, (smiley as GIFPlayer).maxHeight, formating );
					} else
					{
						smiley = Sprite ( new mapper[currentString]());
						element = new GraphicElement ( smiley, smiley.width, /*smiley.height*/standardHeight, formating );
					}
					
				} else element = new TextElement ( currentString, formating );
				groupVector.push(element);
			}
			
			groupElement.setElements(groupVector);
			textBlock.content = groupElement;
			createLines ( textBlock );		
		}
		
		private function createLines (textBlock:TextBlock):void 
		{
			var line:TextLine = textBlock.createTextLine (null, paragraphWidth);
			var yPos:int = 0;
		
			while (line != null)
			{
				addChild(line);
				if (!firstLine)
				{
					firstLine = line;
					yPos += line.height;
				}
				else
				{
					yPos += line.height + Y_OFFSET;
				}
				line.x = X_OFFSET;
				line.y = yPos;
				line = textBlock.createTextLine(line, paragraphWidth);
			}
		}
	}	
}